#!/bin/bash
# getopts.sh - Using getopts

# test-Usage:./getopts.sh -x -y sdfsfd -z sdfsf * 
#               ./getopts.sh -x -y sdfsfd -z sdfsf
#############################

# getopts 只接受单字符的选项，
# 下面的循环，当命令中检测到连字符'-'后，会将其后的单个字符提出，
# 如命令行中的-x的x，如果x后面要接数值的话,则getopts中的x后面要有":"，
# 然后命令行中的-x后面的数值赋值给$OPTARG
while getopts ":xy:z:" opt;
do
    case $opt in
        x)  xopt="-x set and called with $OPTARG"
            echo $xopt;;
        y)  yopt="-y set and called with $OPTARG"
            echo $yopt;;
        z)  zopt="-z set and called with $OPTARG"
            echo $zopt;;
        ?) echo 'USAGE: getopts.sh [-x] [-y arg] [-z arg] file ...'
           exit 1
    esac
done
################### 以下代码涉及到shift的使用，暂不理解
#echo $OPTIND
#shift $(($OPTIND - 1))

#echo "${xopt}: -did not use -x"
#echo "${yopt}: -did not use -y"
#echo "${zopt}: -did not use -z"
#####################
echo "Remaining command-line arguments are:"
for f in "$@"
do
    echo -e "\t$f"
done
